﻿using System;
using BO.News;
using BO.Infrastructure.StaticData;
using System.Data;
using BO.Infrastructure.Utils;
using GUI.Extensions;

namespace GUI.Controls.News
{
    public partial class FocusNewsAtTopOfHomePage : System.Web.UI.UserControl
    {
        const int Top = 10;
        private const string Format = "<li><span class=\"liststyle6 sprite fl\"></span><span class=\"fl time\">{3}</span><a href=\"{0}\" title=\"{2}\">{1}</a></li>";
        private static readonly bool EnableCacheHtml = true && CacheSettingExtensions.EnableCacheHtml;
        protected void Page_Load(object sender, EventArgs e)
        {
            var bo = EnableCacheHtml ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
            var dt = bo.GetListNews(1, Top);
                Visible = DataBind(dt);
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new System.Text.StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    sb.Append(string.Format(Format,
                                        r[SqlConstants.NewsPublish.Url],
                                        r[SqlConstants.NewsPublish.Title],
                                        r[SqlConstants.NewsPublish.EncodedTitle],
                                        PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString()).ToString("HH:mm")));
                }
                ltrData.Text = sb.ToString();
                return true;
            }
            return false;
        }
    }
}